
# 邻接矩阵
def build_graph(input_file):
    """
    input_file:
        4 5
        A B 
        B D 
        C A 
        C D 
        D B
    """
    # 节点分别对应矩阵的下标
    map = {'A': 0, 'B': 1, 'C': 2, 'D': 3}

    with open(input_file) as f:
        # 读头部获取顶点个数和边的个数
        header = f.readline()
        nodes, edges = header.strip().split()
        nodes = int(nodes)

        # 初始化矩
        graph = [[0]*nodes for i in range(nodes)]

        # 相邻节点设置为的值设置为1
        for line in f.readlines():
            u, v = line.strip().split()
            graph[map[u]][map[v]] = 1

        return graph

for row in build_graph("graph.txt"):
    print(row)